package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.ActivitySelfTeamAchieveBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.SelfTeamAchieveEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamAchieveAdapter;
import com.sgb.kjwl.viewmodel.self.packers.SelfTeamAchieveModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 * @author WYJ
 * @date 2020/8/12
 * description:个人中心-施工业绩
 */
public class SelfTeamAchieveActivity extends AppActivity implements SelfTeamAchieveAdapter.IRecyclerItemClick {
    public static final int REQUEST_CODE = 101;
    public static final int REQUEST_ADD_CODE = 102;

    @Inject
    public SelfTeamAchieveModel mModel;
    private ActivitySelfTeamAchieveBinding mBind;
    private SelfTeamAchieveAdapter mAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, SelfTeamAchieveActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                SelfTeamAchieveEditActivity.start(SelfTeamAchieveActivity.this, null, REQUEST_ADD_CODE);
            }
        });
    }

    @Override
    protected void initView() {
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_self_team_achieve);
        mBind.setLifecycleOwner(this);
        mBind.setViewModel(mModel);
        mAdapter = new SelfTeamAchieveAdapter(this, new ArrayList<>());
        mBind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBind.recyclerView.setAdapter(mAdapter);
        mAdapter.setItemClick(this);
    }

    @Override
    protected void init() {
        if (mAdapter.getCount() == 0) {
            DialogHelper.showProgressDialog(this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
            mModel.refreshAndLoadMore(true);
        }
    }

    public void updateViews(List<SelfTeamAchieveEntity> data, int curPager) {
        mBind.recyclerView.refreshComplete();
        mBind.recyclerView.loadMoreComplete();
        DialogHelper.dismissProgressDialog();
        if (curPager == 1) {
            if (data == null || data.size() == 0) {
                mAdapter.clearDatas();
                mBind.emptyView.setVisibility(View.VISIBLE);
                return;
            }
            mBind.emptyView.setVisibility(View.GONE);
            mAdapter.setDatas(data);
        } else {
            if (data != null) {
                mAdapter.addDatas(data);
            }
        }
        mBind.recyclerView.setLoadingMoreEnabled(mAdapter.getItemCount() >= curPager * AppConstant.PAGER_LIMIT);
    }

    public void deleteUpdateView(SelfTeamAchieveEntity entity) {
        List<SelfTeamAchieveEntity> entities = mAdapter.getDatas();
        assert entities != null;
        boolean remove = mAdapter.getDatas().remove(entity);
        if (remove) {
            mAdapter.notifyDataSetChanged();
        }
        if (mAdapter.getItemCount() == 0){
            showEmpty();
        }
    }

    public void showEmpty() {
        DialogHelper.dismissProgressDialog();
        mBind.emptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void delete(SelfTeamAchieveEntity entity) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("确认删除？")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        DialogHelper.showProgressDialog(SelfTeamAchieveActivity.this, null, "数据删除中...", 0, false, null).setCanceledOnTouchOutside(false);
                        mModel.deleteSelfTeamAchieve(entity);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();

    }

    @Override
    public void edit(SelfTeamAchieveEntity entity) {
        SelfTeamAchieveEditActivity.start(this, entity, REQUEST_ADD_CODE);
    }

    @Override
    public void onItemClick(SelfTeamAchieveEntity entity) {
        DialogHelper.showProgressDialog(SelfTeamAchieveActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("constructionAchievementsDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + entity.getNo() + "&isdele=1"+"&type=1";
                    WebViewX5HostActivity.start(SelfTeamAchieveActivity.this, url, SelfTeamProjectActivity.REQUEST_CODE);
                } else {
                    MToast.showToast(SelfTeamAchieveActivity.this, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE || requestCode == REQUEST_ADD_CODE) {
                mModel.refreshAndLoadMore(true);
            }
        }
    }
}
